<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑组件'">
    <template #body>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">名称标识</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.name" class="d-form-input" type="text" placeholder="输入英文、中划线、下划线，例：vue" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">中文名称</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.text" class="d-form-input" type="text" placeholder="输入中英文，例：vue框架" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">版本</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.version" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">类型</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.cat" class="d-form-select">
              <option v-for="item in typeList" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
      </div>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">视图</label>
          </div>
          <div class="d-form-el d-relative">
            <el-tabs v-model="activeView" class="coder-paner el-tabs-paner">
              <el-tab-pane label="编码" name="first">
                <Coder v-model="state.mainCode" :height="'500px'" language="html" />
              </el-tab-pane>
              <el-tab-pane label="地址" name="second">
                <div class="d-section">
                  <el-input v-model="state.main" placeholder="请输入网络地址"></el-input>
                </div>
              </el-tab-pane>
            </el-tabs>
            <el-button @click="handlePreview" class="btn-run" type="primary" plain size="small">运行</el-button>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">预览</label>
          </div>
          <div class="d-form-el">
            <div class="coder-preview">
              <div ref="previewView" class="preview-view">
                <component :is="viewComp"></component>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">属性</label>
          </div>
          <div class="d-form-el d-relative">
            <el-tabs v-model="activeProp" class="coder-paner el-tabs-paner">
              <el-tab-pane label="编码" name="first">
                <Coder v-model="state.propertyCode" :height="'500px'" language="html" />
              </el-tab-pane>
              <el-tab-pane label="地址" name="second">
                <div class="d-section">
                  <el-input v-model="state.property" placeholder="请输入网络地址"></el-input>
                </div>
              </el-tab-pane>
            </el-tabs>
            <el-button @click="handlePreviewProperty" class="btn-run" type="primary" plain size="small">运行</el-button>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">预览</label>
          </div>
          <div class="d-form-el">
            <div class="coder-preview">
              <div ref="previewViewProperty" class="preview-view">
                <component :is="propertyComp"></component>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">图标</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.icon" class="d-form-input" type="text" placeholder="base64编码或图片地址" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-12">
          <div class="d-form-label d-text-right">
            <label class="label-text">描述</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.description" class="d-form-textarea" rows="6"></textarea>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">状态</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.status" class="d-form-select">
              <option v-for="item in statusList" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">标签</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.labels" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">排序</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.order" class="d-form-input" type="text" placeholder="数值越大越靠前" />
          </div>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import Vue from 'vue'
import { PanerForm } from '@daelui/vdog/dist/pages.js'
import { Dialoger, FormCalendar, FormSelecter, Tabs, TabsItem } from '@daelui/vdog/dist/components.js'
import { Coder } from '@daelui/vdog/dist/coder.js'
import { zh as locale } from '@daelui/dogjs/dist/locale.js'
import { objecter, parser } from '@daelui/dogjs/dist/components.js'
import coder from '@daelui/dogjs/dist/libs/coder.js'
import { STATUS } from '@daelui/dogjs/dist/constant.js'
import tpl from '../page/components/vue/default'
import { state } from '../../../service/stock/index.js'
import vcSer from '../../../service/action/viewcomp.action'

import * as Babel from '@babel/standalone'
const win = typeof self !== 'undefined' ? window : global
win.Babel = Babel

export default {
  components: {
    PanerForm,
    FormCalendar,
    Dialoger,
    FormSelecter,
    Coder,
    Tabs, TabsItem
  },

  data () {
    return {
      STATUS,
      isEdit: false, // 是否是编辑模式
      activeView: 'first',
      activeProp: 'first',
      viewComp: 'div',
      propertyComp: 'div',
      // 表单数据
      state: {
        id: '',
        text: '',
        name: '',
        cat: 'vue',
        type: 'component',
        main: '',
        property: '',
        mainCode: tpl.code,
        propertyCode: tpl.property,
        description: '',
        version: '1.0.0',
        labels: '',
        status: 1,
        order: 1,
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      // 类型列表
      typeList: state.codeLangTypeList,
      // 状态列表
      statusList: state.statusList
    }
  },

  methods: {
    /**
     * @function 加载数据
    */
    loadItem (id) {
      vcSer.queryItem({ id }).then(result => {
        let data = result.data || {}
        this.state = data
        if (data.main) {
          this.activeView = 'second'
        }
        if (data.property) {
          this.activeProp = 'second'
        }
      })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      let state = objecter.clone(this.state)
      delete this.state.createTime
      // 编辑
      if (this.isEdit) {
        defer = vcSer.editItem(state)
      } else { // 添加
        defer = vcSer.addItem(state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
          // 添加则回到列表页
          if (!this.isEdit) {
            this.$router.push(this.getMicroRoute('/dview/component/list'))
          }
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    },

    /**
     * @function 重置
    */
    handleReset () {},

    /**
     * @function 预览
    */
    handlePreview () {
      if (this.state.mainCode) {
        coder.vue.resolve(this.state.mainCode, Vue, $pig).then(cp => {
          this.viewComp = cp
        })
      } else if (this.state.main) {
        $pig.import(this.state.main).then(res => {
          this.viewComp = res.default
        })
      }
    },

    /**
     * @function 预览
    */
    handlePreviewProperty () {
      if (this.state.propertyCode) {
        coder.vue.resolve(this.state.propertyCode, Vue, $pig).then(cp => {
          this.propertyComp = cp
        })
      } else if (this.state.property) {
        $pig.import(this.state.property).then(res => {
          this.propertyComp = res.default
        })
      }
    }
  },

  mounted () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
      this.handlePreview()
      this.handlePreviewProperty()
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
  }
}
</script>

<style lang="less" scoped>
.d-form-label {
  width: 90px;
}
.coder-paner {
  /deep/.d-paner-item-content {
    height: 500px;
  }
}
.coder-preview {
  overflow: auto;
  padding: 8px;
  height: 536px;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
}
.preview-view {
  overflow: auto;
  height: 100%;
}
.btn-run {
  position: absolute;
  right: 0;
  top: 0;
}
</style>
<style lang="less">
.d-form-product .d-editor-container {
  height: 442px;
  .quill-editor {
    height: 400px
  }
}
</style>
